using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using PlacementManager;
using AjaxControlToolkit;


/// <summary>
/// Summary description for Geo_LocationService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Geo_LocationService : System.Web.Services.WebService {

    public Geo_LocationService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    /// <summary>
    /// AJAX web service to return countries to an AJAX Toolkit CascadingDropDown.
    /// </summary>
    /// <param name="knownCategoryValues"></param>
    /// <param name="category"></param>
    /// <returns>List of countries.</returns>
    [WebMethod]
    public AjaxControlToolkit.CascadingDropDownNameValue[] GetCountries(string knownCategoryValues, string category)
    {
        PlacementManager.Geo_CountryComponent oCountryComponent = new Geo_CountryComponent();
        List<PlacementManager.Geo_Country> oCountries = oCountryComponent.SelectAll("CountryName");

        List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();

        foreach (PlacementManager.Geo_Country Country in oCountries)
        {
            values.Add(new CascadingDropDownNameValue(Country.CountryName, Country.CountryID.ToString()));
        }

        return values.ToArray();
    }

    /// <summary>
    /// AJAX web service to return states to an AJAX Toolkit CascadingDropDown.
    /// </summary>
    /// <param name="knownCategoryValues"></param>
    /// <param name="category"></param>
    /// <returns>List of states.</returns>
    [WebMethod]
    public AjaxControlToolkit.CascadingDropDownNameValue[] GetStates(string knownCategoryValues, string category)
    {
        StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(
          knownCategoryValues);
        int CountryID;
        if (!kv.ContainsKey("Country") ||
            !Int32.TryParse(kv["Country"], out CountryID))
        {
            return null;
        }

        PlacementManager.Geo_AdministrativeDivisionComponent oAdminDivisionComponent = new Geo_AdministrativeDivisionComponent();
        List<PlacementManager.Geo_AdministrativeDivision> oStates = oAdminDivisionComponent.SelectAllWCountryID(CountryID, "CountryName");

        List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();

        foreach (PlacementManager.Geo_AdministrativeDivision State in oStates)
        {
            values.Add(new CascadingDropDownNameValue(State.DivisionName, State.AdministrativeDivisionID.ToString()));
        }

        return values.ToArray();

    }

    /// <summary>
    /// AJAX web service returns a list of towns/cities for to an AJAX Toolkit CascadingDropDown.
    /// </summary>
    /// <param name="knownCategoryValues"></param>
    /// <param name="category"></param>
    /// <returns>List of towns/cities</returns>
    [WebMethod]
    public AjaxControlToolkit.CascadingDropDownNameValue[] GetTownsCities(string knownCategoryValues, string category)
    {
        StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(
          knownCategoryValues);
        int AdministrativeDivisionID;
        if (!kv.ContainsKey("AdministrativeDivision") ||
            !Int32.TryParse(kv["AdministrativeDivision"], out AdministrativeDivisionID))
        {
            return null;
        }

        PlacementManager.Geo_LocationComponent oGeoLocationComponent = new Geo_LocationComponent();
        List<PlacementManager.Geo_Location> oLocations = oGeoLocationComponent.SelectAllWAdministrativeDivisionID(AdministrativeDivisionID, "FullName");

        List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();

        foreach (PlacementManager.Geo_Location oLocation in oLocations)
        {
            values.Add(new CascadingDropDownNameValue(oLocation.FullName, oLocation.GeoLocationID.ToString()));
        }

        return values.ToArray();

    }

    /// <summary>
    /// Returns an array of locations that begin with specific letters. This is used in an AJAX autosuggest control.
    /// </summary>
    /// <param name="prefixText">The starting letters of the location.</param>
    /// <param name="count">The number of results to return.</param>
    /// <returns></returns>
    [WebMethod]
    public string[] GetLocations(string prefixText, int count, string contextKey)
    {
        if (count == 0)
        {
            count = 10;
        }


        //Get a string list to return
        List<string> items = new List<string>(count);


        PlacementManager.DAL.Geo_Location_DAL oDALGeoLoc = new PlacementManager.DAL.Geo_Location_DAL();
        DataSet ds = oDALGeoLoc.SelectAllStartingWith(prefixText);
        //Loop through the locations list and add the first x number to the string list
        int i = 0;
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            items.Add(dr["FullNameAndState"].ToString());
            i++;
            if (i > count)
            {
                break;
            }
        }


        return items.ToArray();
    }
    [WebMethod]
    public string[] GetLocationsForState(string prefixText, int count, string contextKey)
    {
        if (count == 0)
        {
            count = 10;
        }


        //Get a string list to return
        List<string> items = new List<string>(count);


        PlacementManager.DAL.Geo_Location_DAL oDALGeoLoc = new PlacementManager.DAL.Geo_Location_DAL();
        DataSet ds = oDALGeoLoc.SelectAllWAdministrativeDivisionIDStartingWith(prefixText, Int32.Parse(contextKey));
        //Loop through the locations list and add the first x number to the string list
        int i = 0;
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            items.Add(dr["FullName"].ToString());
            i++;
            if (i > count)
            {
                break;
            }
        }


        return items.ToArray();
    }
    
}

